﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.Global;
using CE.AreaCountry;

public partial class Admin_area_country_AddNewCountry : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null)
        {
            Response.Redirect("~/Admin/AdminSecureLogin.aspx");
        }

        if (!Page.IsPostBack)
        {
            
            if (Request.Params["COId"] != null) 
            {
                Int64 _coid;
                if (Int64.TryParse(Request.Params["COId"].ToString(), out _coid))
                {

                   ShowCountryDetails(_coid);
                }
            }
            else
                clearProcess();
        }

    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        clearProcess();
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ViewCountry.aspx");
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            CountryInfo _countryinfo = new CountryInfo();
            CountryInfoDB _countryinfodb = new CountryInfoDB();

            _countryinfo.CountryId = Convert.ToInt64(lblCoId.Text.Trim());
            _countryinfo.Description = txtCountry.Text.Trim();
            _countryinfo.Status = Convert.ToBoolean(chkCountryStatus.Checked.ToString());

            _countryinfo.CountryId = _countryinfodb.Insert(_countryinfo);

            if (_countryinfo.CountryId > 0)
            {
                clearProcess();
                lblMessage.Text = "Saved successfully!";
            }
            else
            {
                lblError.Text = "Saving failed!";
            }
        }
    }

    public void clearProcess()
    {
        lblCoId.Text = "0";
        txtCountry.Text = "";

        btnSubmit.Text = "Submit";
        btnClear.Text = "Clear";
    }

    private void ShowCountryDetails(Int64 CoId)
    {
        CountryInfo _countryinfo = new CountryInfo();
        CountryInfoDB _countryinfodb = new CountryInfoDB();
        _countryinfo = _countryinfodb.GetCountryInfo(CoId);

        if (_countryinfo != null)
        {
            lblCoId.Text = _countryinfo.CountryId.ToString();
            txtCountry.Text = _countryinfo.Description.ToString();
            chkCountryStatus.Checked = Convert.ToBoolean(_countryinfo.Status);

            btnSubmit.Text = "Update";
            btnClear.Text = "Add New";

        }

    }
}
